import pytest
from _pytest.nodes import Item
from pages.login_page import LoginPage
import allure


@pytest.fixture(scope='session', autouse=True)
def login_first(browser, base_url, pytestconfig):
    """登录auth"""
    context = browser.new_context(base_url=base_url)
    page = context.new_page()
    login_page = LoginPage(page)
    login_page.navigate()
    login_page.page.locator('#login_tologin').click()
    login_page.login('真实账号', '真实密码')
    # 保存登录cookie
    storage_path = pytestconfig.rootpath.joinpath('auth/login_cookie.json')
    context.storage_state(path=storage_path)
    login_page.page.wait_for_timeout(0.5)
    context.close()


@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright, pytestconfig):
    """
    添加context 上下文参数，默认每个页面加载cookies
    :param browser_context_args:
    :param playwright:
    :return:
    """
    return {
        "storage_state": pytestconfig.rootpath.joinpath("auth/login_cookie.json"),
        **browser_context_args,
    }

@pytest.fixture(scope='module')
def admin_context(browser,base_url,pytestconfig):
    context = browser.new_context(
        base_url=base_url,
        no_viewport = True,
        storage_state = pytestconfig.rootpath.joinpath("auth/admin.json")
    )
    yield context
    context.close()
